﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using Newtonsoft.Json.Linq;

namespace InfoReaderLib.Tester
{
    class Util
    {
        /// <summary>
        /// Obtain and parse json file, then read text from another file, finally write text into the speicified field in the first json file
        /// </summary>
        /// <returns>Parsed json with jsonField filed with content from another Filename</returns>
        async public static Task<string> FillJSONObjectFromAnotherFileAsync(string jsonFilename, Dictionary<string, string> jsonFieldAndFile)
        {
            // http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx#BKMK_HowtoWriteanAsyncMethod
            string returnJSON = null;
            try
            {
                var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
                var file = await folder.GetFileAsync(jsonFilename);
                if (file == null)
                    return null;

                var jsonString = await Windows.Storage.FileIO.ReadTextAsync(file);
                if (jsonString == null)
                {
                    Util.Debug("failed to load json file " + file);
                    return null;
                }

                var json = (JObject)JsonConvert.DeserializeObject(jsonString);

                // loop through dictionary to fill in all specified fields from other files
                foreach (var fieldAndFile in jsonFieldAndFile)
                {
                    string jsonField = fieldAndFile.Key;
                    string anotherFilename = fieldAndFile.Value;

                    var anotherFile = await folder.GetFileAsync(anotherFilename);
                    if (anotherFile == null)
                    {
                        Util.Debug("failed to find file which contains json field value " + anotherFile);
                        continue;
                    }

                    var anotherFileString = await Windows.Storage.FileIO.ReadTextAsync(anotherFile);
                    // to one line
                    anotherFileString = anotherFileString.Replace("\n", "").Replace("\r", "");

                    if (anotherFileString != null)
                        json[jsonField] = anotherFileString;
                }

                returnJSON = json.ToString();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.StackTrace);
                throw ex;
            }
            return returnJSON;
        }

        /// <summary>
        /// Dump current time
        /// </summary>
        public static void DebugTime()
        {
            Debug(DateTime.Now.ToString("HH:mm:ss"));
        }

        /// <summary>
        /// Debug console
        /// </summary>
        /// <param name="message"></param>
        public static void Debug(object message)
        {
            System.Diagnostics.Debug.WriteLine(message);
        }
    }
}
